


//
//  RMediaSearchResulVC.m
//  ZQLearn
//
//  Created by macfeel on 2019/5/13.
//  Copyright © 2019 macfeel. All rights reserved.
//
#import "ZNewsDetailViewController.h"
#import "RMediaSearchResulVC.h"
#import "ZBookMsgDetailVC.h"
#import "ReadModel.h"
#import "ZHomeSearchResultVC.h"
#import "SearchBar.h"
#import "OrderCell.h"//借用订单cell
#import "HomeModel.h"
#import "NewsCell.h"
@interface RMediaSearchResulVC ()
<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)SearchBar *searchBar;
@end
#define cellIdentifier @"news"
@implementation RMediaSearchResulVC

#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(60, 7, ScreenWidth-120, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    bar.souImg.hidden = YES;
    bar.barBtn.hidden = YES;
    bar.textFd.hidden = false;
    bar.textFd.delegate = self;
    self.searchBar = bar;
    //    self.navigationItem.titleView = bar;
    [self.navigationController.navigationBar addSubview:bar];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.searchBar.hidden = false;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchBar.hidden = YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 105.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //背景蓝
    //    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
    [self.navigationItem setItemWithTitle:@"搜索结果" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 45, 45) title:@"搜索" Image:@"" selectedIMG:@"" tartget:self action:@selector(sousuoAction)];
    
    [self addSearchBar];
    self.dataArray = [NSMutableArray array];

    [self.view addSubview:self.tableView];
    
    //    [self loadData];
}

- (void)loadData{
    //   NSDictionary *dic = @{@"name":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"30",};
    NSDictionary *dic = @{@"title":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"100",};
    NSString *url = kNewsListUrl;
    [ZNetworkService postRequestWithBaseUrl:url setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                NewsModel *model = [NewsModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
             [self.searchBar.textFd resignFirstResponder];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

///
- (void)sousuoAction{
    ///搜索接口请求
    [self.dataArray removeAllObjects];
    [self loadData];
}

#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *model;
    if (self.dataArray.count>indexPath.row) {
        model =  self.dataArray[indexPath.row];
    }
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell configCellWithData:model];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //正常的新闻详情
    NewsModel *model = self.dataArray[indexPath.row];
    ZNewsDetailViewController *news = [ZNewsDetailViewController new];
    //        news.htmlString = model.content;
    news.model = model;
    [self.navigationController pushViewController:news animated:YES];
}


@end
